// pages/manageClinicInfo/manageClinicInfo.js
Page({
  data: {
    clinicPic: {
      clinicPicHome: [],
      clinicPicClinic: []
    },
  },

  //从数据库获取诊所信息
  onLoad() {
    wx.cloud.callFunction({
      name: "clinicPic",
      data: {
        action: "find",
      }
    }).then(res => {
      console.log('---获取数据库中的诊所图片成功', res)
      this.setData({
        clinicPic: res.result.data[0]
      })
      console.log('---获取数据库诊所图片后本地的诊所图片', this.data.clinicPic)
    }).catch(res => {
      console.log('---获取数据库中的诊所图片失败', res)
    })
  },

  //添加主页轮播图(0选择1云存储2本地3云数据库)
  addPicHome() {
    let that = this
    //0选择图片
    wx.chooseImage({
      success(res) {
        console.log("0选择图片成功，选择的图片的信息", res)
        const ImageUrl = res.tempFilePaths[0] //图片临时地址
        let suffix = /\.[^\.]+$/.exec(ImageUrl)[0]; // 正则表达式，获取图片扩展名
        console.log('0选择的图片的临时url', ImageUrl)
        console.log('0选择的图片文件扩展名', suffix)
        //1把这张图传到云存储
        wx.cloud.uploadFile({
            count: 1,
            cloudPath: 'clinicPicHome/' + new Date().getTime() + suffix,
            filePath: ImageUrl
          },
          //1上传中提示
          wx.showLoading({
            title: '上传中...',
          })
        ).then(res => {
          console.log('1上传图片到云存储成功，图片的id是', res.fileID)
          //2把这张图片的地址和云存储id传到本地
          var i = that.data.clinicPic.clinicPicHome.length //添加图片之前的图片数量
          var clinicPicHomeId = 'clinicPic.clinicPicHome[' + i + '].fileId'
          that.setData({
            // [clinicPicHomeUrl]: ImageUrl,
            [clinicPicHomeId]: res.fileID,
          })
          console.log('2添加图片后的本地诊所信息', that.data.clinicPic)
          //3把这张图的云存储id地址传到云数据库
          wx.cloud.callFunction({
            name: "clinicPic",
            data: {
              action: "changeClinicPicHome",
              clinicPicHome: that.data.clinicPic.clinicPicHome,
            }
          }).then(res => {
            console.log('3上传这张图片到云数据库成功', res)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  title: '上传图片成功',
                })
              },
            })

          }).catch(res => {
            console.log('3上传这张图片到云数据库失败', res)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  icon: 'none',
                  title: '上传图片失败',
                })
              },
            })
          })
        }).catch(error => {
          console.log('1上传图片到云存储失败', error)
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                icon: 'none',
                title: '上传图片失败',
              })
            },
          })
        })
      }
    })
  },

  //添加诊所页轮播图(0选择1云存储2本地3云数据库)
  addPicClinic() {
    let that = this
    //0选择图片
    wx.chooseImage({
      success(res) {
        console.log("0选择图片成功，选择的图片的信息", res)
        const ImageUrl = res.tempFilePaths[0] //图片临时地址
        let suffix = /\.[^\.]+$/.exec(ImageUrl)[0]; // 正则表达式，获取图片扩展名
        console.log('0选择的图片的临时url', ImageUrl)
        console.log('0选择的图片文件扩展名', suffix)
        //1把这张图传到云存储
        wx.cloud.uploadFile({
            count: 1,
            cloudPath: 'clinicPicClinic/' + new Date().getTime() + suffix,
            filePath: ImageUrl
          },
          //1上传中提示
          wx.showLoading({
            title: '上传中...',
          })
        ).then(res => {
          console.log('1上传图片到云存储成功，图片的id是', res.fileID)
          //2把这张图片的地址和云存储id传到本地
          var i = that.data.clinicPic.clinicPicClinic.length //添加图片之前的图片数量
          var clinicPicClinicId = 'clinicPic.clinicPicClinic[' + i + '].fileId'
          that.setData({
            // [clinicPicClinicUrl]: ImageUrl,
            [clinicPicClinicId]: res.fileID,
          })
          console.log('2添加图片后的本地诊所信息', that.data.clinicPic)
          //3把这张图的云存储id地址传到云数据库
          wx.cloud.callFunction({
            name: "clinicPic",
            data: {
              action: "changeClinicPicClinic",
              clinicPicClinic: that.data.clinicPic.clinicPicClinic,
            }
          }).then(res => {
            console.log('3上传这张图片到云数据库成功', res)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  title: '上传图片成功',
                })
              },
            })
          }).catch(res => {
            console.log('3上传这张图片到云数据库失败', res)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  icon: 'none',
                  title: '上传图片失败',
                })
              },
            })
          })
        }).catch(error => {
          console.log('1上传图片到云存储失败', error)
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                icon: 'none',
                title: '上传图片失败',
              })
            },
          })
        })
      }
    })
  },

  //删除主页轮播图（00删除确认11云存储22本地图片33数据库图片信息）
  delPicHome(e) {
    console.log('picid', e.currentTarget.dataset.picid)
    // console.log('picurl', e.currentTarget.dataset.picurl)
    let that = this
    //00删除确认
    wx.showModal({
      title: '提示',
      content: '确定要删除这张图片吗',
      success(res) {
        if (res.confirm) {
          console.log('00用户点击确定')
          //00删除中提示
          wx.showLoading({
            title: '正在删除...',
          })
          //11删除云存储文件
          wx.cloud.deleteFile({
            fileList: [e.currentTarget.dataset.picid]
          }, ).then(res => {
            console.log('11删除云文件成功', res.fileList)
            //22删除本地图片
            var clinicPicHome = "clinicPic.clinicPicHome"
            for (var i = 0; i < that.data.clinicPic.clinicPicHome.length; i++) {
              if (that.data.clinicPic.clinicPicHome[i].fileId === e.currentTarget.dataset.picid) {
                that.data.clinicPic.clinicPicHome.splice(i, 1)
                that.setData({
                  [clinicPicHome]: that.data.clinicPic.clinicPicHome
                })
              }
            }
            console.log('22删除后本地图片数组', that.data.clinicPic.clinicPicHome)
            //33将新的图片数据上传到数据库
            wx.cloud.callFunction({
              name: "clinicPic",
              data: {
                action: "changeClinicPicHome",
                clinicPicHome: that.data.clinicPic.clinicPicHome,
              }
            }).then(res => {
              console.log('33删除这张图片到云数据库成功', res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    title: '删除图片成功',
                  })
                },
              })
            }).catch(res => {
              console.log('33删除这张图片到云数据库失败', res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    icon: 'none',
                    title: '删除图片失败',
                  })
                },
              })
            })
          }).catch(error => {
            console.log('11删除云文件失败', error)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  icon: 'none',
                  title: '删除图片失败',
                })
              },
            })
          })
        } else if (res.cancel) {
          console.log('00用户点击取消')
        }
      }
    })
  },

  //删除诊所轮播图（00删除确认11云存储22本地图片33数据库图片信息）
  delPicClinic(e) {
    console.log('picid', e.currentTarget.dataset.picid)
    // console.log('picurl', e.currentTarget.dataset.picurl)
    let that = this
    //00删除确认
    wx.showModal({
      title: '提示',
      content: '确定要删除这张图片吗',
      success(res) {
        if (res.confirm) {
          console.log('00用户点击确定')
          //00删除中提示
          wx.showLoading({
            title: '删除中...',
          })
          //11删除云存储文件
          wx.cloud.deleteFile({
            fileList: [e.currentTarget.dataset.picid]
          }).then(res => {
            console.log('11删除云文件成功', res.fileList)
            //22删除本地图片
            var clinicPicClinic = "clinicPic.clinicPicClinic"
            for (var i = 0; i < that.data.clinicPic.clinicPicClinic.length; i++) {
              if (that.data.clinicPic.clinicPicClinic[i].fileId === e.currentTarget.dataset.picid) {
                that.data.clinicPic.clinicPicClinic.splice(i, 1)
                that.setData({
                  [clinicPicClinic]: that.data.clinicPic.clinicPicClinic
                })
              }
            }
            console.log('22删除后本地图片数组', that.data.clinicPic.clinicPicClinic)
            //33将新的图片数据上传到数据库
            wx.cloud.callFunction({
              name: "clinicPic",
              data: {
                action: "changeClinicPicClinic",
                clinicPicClinic: that.data.clinicPic.clinicPicClinic,
              }
            }).then(res => {
              console.log('33删除这张图片到云数据库成功', res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    title: '删除图片成功',
                  })
                },
              })
            }).catch(res => {
              console.log('33删除这张图片到云数据库失败', res)
              wx.hideLoading({
                success: (res) => {
                  wx.showToast({
                    icon: 'none',
                    title: '删除图片失败',
                  })
                },
              })
            })
          }).catch(error => {
            console.log('11删除云文件失败', error)
            wx.hideLoading({
              success: (res) => {
                wx.showToast({
                  icon: 'none',
                  title: '删除图片失败',
                })
              },
            })
          })
        } else if (res.cancel) {
          console.log('00用户点击取消')
        }
      }
    })
  },
})